package com.scuec.trainingmanagementsys.controller;
import com.scuec.trainingmanagementsys.entity.Team;
import com.scuec.trainingmanagementsys.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/team")
public class TeamController {
    @Autowired
    private TeamService teamService;

    // 添加团队
    @PostMapping("/insert")
    public Map<String, Object> addTeam(@RequestBody Team team) {
        Map<String, Object> result = new HashMap<>();
        int rows = teamService.addTeam(team);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "团队添加成功");
        } else {
            result.put("success", false);
            result.put("message", "团队添加失败");
        }
        return result;
    }

    // 删除团队
    @DeleteMapping("delete/{teamID}")
    public Map<String, Object> deleteTeam(@PathVariable Integer teamID) {
        Map<String, Object> result = new HashMap<>();
        int rows = teamService.deleteTeam(teamID);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "团队删除成功");
        } else {
            result.put("success", false);
            result.put("message", "团队删除失败");
        }
        return result;
    }

    // 更新团队
    @PutMapping("/update/{teamID}")
    public Map<String, Object> updateTeam(@RequestBody Team team) {
        Map<String, Object> result = new HashMap<>();
        int rows = teamService.updateTeam(team);
        if (rows > 0) {
            result.put("success", true);
            result.put("message", "团队更新成功");
        } else {
            result.put("success", false);
            result.put("message", "团队更新失败");
        }
        return result;
    }

    // 获取单个团队（不包含关联数据）
    @GetMapping("find/{teamID}")
    public Team getTeam(@PathVariable Integer teamID) {
        return teamService.getTeam(teamID);
    }

    // 获取所有团队（不包含关联数据）
    @GetMapping("/list")
    public List<Team> getAllTeams() {
        return teamService.getAllTeams();
    }

    // 获取团队成员（包含成员姓名和角色）
    @GetMapping("/members/{teamID}")
    public List<Map<String, Object>> getTeamMembers(@PathVariable Integer teamID) {
        return teamService.getTeamMembers(teamID);
    }

    // 获取关联查询后的团队数据（包含 projectName 和 leaderName）
    @GetMapping("/listWithRelations")
    public List<Team> listWithRelations(@RequestParam(required = false) String teamName,
                                        @RequestParam(required = false) String projectName,
                                        @RequestParam(required = false, defaultValue = "1") int page,
                                        @RequestParam(required = false, defaultValue = "10") int size) {
        // 这里可以添加分页逻辑，目前简单返回所有数据
        return teamService.listWithRelations();
    }

    //根据ProjectID获取信息
    @GetMapping("/getTeamsByProjectId/{projectId}")
    public Map<String, Object> getTeamsByProjectId(@PathVariable Integer projectId) {
        Map<String, Object> result = new HashMap<>();
        List<Team> teams = teamService.getTeamsByProjectId(projectId);
        result.put("code", 200);
        result.put("data", teams);
        result.put("message", "success");
        return result;
    }
}